package com.example.wxserver;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
public class FileController {
    @PostMapping("/uploadFile")
    @ResponseBody
    public Map<String,Object> uploadFile(@RequestParam("file") MultipartFile file) {
        HashMap<String,Object> map = new HashMap();
        try {
            byte[] bytes = file.getBytes();
            String newFileName = (new Date().getTime()+file.getOriginalFilename());
            Path path = Paths.get("D:\\upload\\"+newFileName);
            Files.write(path,bytes);
            map.put("success",true);
            map.put("msg","成功");
            HashMap<String,Object> data=new HashMap<>();
            data.put("name","http://localhost:8080/"+newFileName);
            map.put("data",data);
        } catch (IOException e) {
            e.printStackTrace();
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;


    }
}
